package com.ctk.wifi.a.bonjour;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.ctk.wifi.R;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;


public class BonjourActivity extends Activity {
	private Button send_bt = null;
	private Button clean_bt = null;
    private TextView recv_tv = null;
    private EditText send_et = null;
    
	final String TAG = "";
	PrinterDiscovery pd;
	int flag = 0;
	
	DiscoverServices mDiscoverServices;
	
	//Handler相关变量
    public static Handler mHandler;
    public static String content = ""; 
    
    //EditText暂存变量
    private String mEditText;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logcat);
        //send_bt = (Button) findViewById(R.id.datatrans_send_button);
        //recv_tv = (TextView)findViewById(R.id.datatrans_receive_textview);
        //send_et = (EditText)findViewById(R.id.datatrans_send_edittext);
        send_bt = (Button) findViewById(R.id.act01_btn);
        recv_tv = (TextView)findViewById(R.id.log_tv);
        clean_bt = (Button) findViewById(R.id.act02_btn);
    	
        pd = PrinterDiscovery.getInstance(getApplicationContext());
        
        new Thread(new Runnable() {
            @Override
            public void run() {
            	displaylog();
            }
        }).start();
        
        
    	send_bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDiscoverServices = new DiscoverServices();
				mDiscoverServices.scan();
				if(flag == 0 ) {
					flag = 1;
					Log.d(TAG, "Resume");
					pd.start();
				}
				else {
					flag = 0;
					pd.stop();
					recv_tv.setText("");
					try {
						Process process1 = Runtime.getRuntime().exec("logcat -c");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.d(TAG, "Paused");
				}
			}
    	});
    }
    
    public void displaylog(){
        
        try {
        		//将所使用的命令用一个数组的形式表示出来
                ArrayList<String> command = new ArrayList<String>();
                command.add("logcat");
                command.add("-d");
                //I 表示I级别的Log信息， 还有S级别 这是两个常用级别
                Process process = Runtime.getRuntime().exec(command.toArray( new String[command.size()]));  //执行数组的命令  
                
                //得到字符串
                BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
                StringBuilder log = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                //将字符串的信息读给line
                		log.append(line);
                        log.append("\n");
                }
                TextView logtv = (TextView) findViewById(R.id.log_tv);
                recv_tv.setText(log.toString()); //将Log加载到Textview中
                final ScrollView scrollView = (ScrollView) findViewById(R.id.logScrollView);
                scrollView.post(new Runnable() {
                        public void run() {
                                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                });
        } catch (IOException e) {

        }        
    }

}